package com.exampl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exampl.common.R;
import com.exampl.domain.Dish;
import com.exampl.domain.DishFlavor;
import com.exampl.domain.Record;
import com.exampl.dto.DishDto;
import com.exampl.service.DishFlavorService;
import com.exampl.service.DishService;
import com.exampl.service.RecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    DishService dishService;

    @Autowired
    RecordService recordService;

    @Autowired
    DishFlavorService dishFlavorService;

    // 好菜推荐
    @GetMapping("/goods")
    public R<List<Dish>> goods(){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<Dish>();
        lqw.orderByDesc(Dish::getSaleNum);
        // 限制查询8条数据，在sql语句后面添加如下
        lqw.last("limit 8");
        List<Dish> list = dishService.list(lqw);
        return R.success(list);
    }

    // 店铺外搜索
    @PostMapping("/search")
    public R<List<Dish>> search(String name, HttpSession session){
        List<Dish> list = new ArrayList<Dish>();
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<Dish>();
        lqw1.eq(name != null, Dish::getName, name);
        Dish dish = dishService.getOne(lqw1);
        // 搜索的菜品排在最前
        list.add(dish);
        LambdaQueryWrapper<Dish> lqw2 = new LambdaQueryWrapper<Dish>();
        lqw2.ne(Dish::getId, dish.getId());
        // 除了搜索的菜品还有2个菜品
        lqw2.last("limit 3");
        list.addAll(dishService.list(lqw2));
        // 获取用户id
        recordService.store(name);
        return R.success(list);
    }

    // 店铺外模糊匹配与店铺内搜索
    @PostMapping("/searchByNames")
    public R<List<DishDto>> searchByName(String name){
        if(name.equals("")){
            return R.success(new ArrayList<DishDto>());
        }
        System.out.println(name);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<Dish>();
        lqw.like(name != null, Dish::getName, name);
        List<Dish> list = dishService.list(lqw);
        List<DishDto> dtoList = list.stream().map((data) -> {
            DishDto dishDto = new DishDto();
            // 将dish中的变量值赋值给dishDto
            BeanUtils.copyProperties(data, dishDto);
            Long id = data.getId();
            // 设置dishDto的口味属性值
            LambdaQueryWrapper<DishFlavor> lqw1 = new LambdaQueryWrapper<DishFlavor>();
            lqw1.eq(DishFlavor::getDishId, id);
            List<DishFlavor> flavors = dishFlavorService.list(lqw1);
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dtoList);
    }

    // 历史记录显示
    @GetMapping("/history")
    public R<String> history(HttpSession session){
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<Record>();
        lqw.eq(Record::getUserId, user_id);
        Record record = recordService.getOne(lqw);
        return R.success(record.getHistory());
    }
}
